#!/usr/bin/perl
#folders
$root_folder="/root/switch_project";
$result_folder="$root_folder/experiment/exp26"; #root dir to store experimental resutls
$aggregated_result="$result_folder/recv_rate.txt"; #results for all experiments, used for plot

$runs=1; # number of repeated experiment (same setting)

#used to calculate gap (not used now)
$packet_size=1500; #byte
$number_of_packets=10*$iteration; #because nf2_dag1.pcap has 10 packets
@speed;
@gap;

#specify sending rate
$start_sending_rate=100;#Mb/s
$end_sending_rate=1000;
$sending_rate_gap=100;

#calculate speed and gap (gap is not used for now)
for($b=$start_sending_rate, $a=0; $b<=$end_sending_rate; $b+=$sending_rate_gap, $a++)
{
	$speed[$a]=$b;
	$gap[$a]=getInterFrameGap($speed[$a], $packet_size, $number_of_packets);
	print "\n speed = $speed[$a], gap = $gap[$a]\n";
}


#open(IN, $ARGV[0]) or die "$ARGV[0] cannot be opened to read.\n";
#open(OUT, ">$ARGV[1]") or die "$ARGV[1] cannot be opened to write.\n";

#post-process data
#for($j=0; $j<=$#speed; $j++)
for($j=8; $j<=8; $j++)
{
	for($k=0; $k<=$#speed; $k++)
	#for($k=2; $k<=2; $k++)
	{
		chdir "$result_folder/$speed[$j]M/$speed[$k]M/1";
		print "Processing $result_folder/$speed[$j]M/$speed[$k]M/1\n";		

		$in_flow="flow_table_sorted.csv";
		$out_delay="delay.txt";
		$out_loss_episode="loss_episode.txt";

		#sort according arrival_timestamp
		system("sort flow_table.csv > $in_flow");

		#open files
		open(IN_FLOW, $in_flow) or die "$in_flow cannot be opened.\n";
		#open(OUT_DELAY, ">$out_delay") or die "$out_delay cannot be opened.\n";
		#open(OUT_LP, ">$out_loss_episode") or die "$out_loss_episode cannot be opened.\n";

		#compute end time
		$line=<IN_FLOW>;
		chomp($line);
		@line_block=split(', ', $line);
		$delay=$line_block[4];
		$arrival_time_start=$line_block[0];
		print OUT_DELAY "$delay\n";
		print "beginning: delay=$delay, arrival_time=$arrival_time_start\n";

		if($flow1_send_interval[$j]<=$flow2_send_interval[$k])
		{
			$end_time = $num_pkts*$flow1_send_interval[$j]+$arrival_time_start;
		}
		else
		{
			$end_time = $num_pkts*$flow2_send_interval[$k]+$arrival_time_start;			
		}
		print "end time = $end_time\n";


while(<IN>)
{
	$line=$_;
	chomp($line);
	
	@block=split(', ', $line);
	if($block[3]!=0)
	{
		print OUT "$line\n";
	}

}

close(IN);
close(OUT);
